﻿using System;
using System.Collections.Generic;

namespace System.Reflection
{
	/// <summary>
	/// Defines Extension-Methods for classes in the <see cref="System.Reflection"/> namespace.
	/// </summary>
	public static class ArgusLibExtensions
	{
		public static TInteface[] GetInterfaceImplementations<TInteface>(this Assembly assembly/*, Type interfaceType*/, params object[] constructorArgs)
		{
			//if (interfaceType.IsInterface == false)
			//	throw new ArgumentException("Interface-Type expected.", "interfaceType");

			Type interfaceType = typeof(TInteface);
			Type[] types = assembly.GetTypes();
			List<TInteface> RetVal = new List<TInteface>();
			string interfaceName = interfaceType.FullName;
			
			Type[] constructorArgTypes = new Type[constructorArgs.Length];
			for (int i = 0; i < constructorArgs.Length; i++)
			{
				constructorArgTypes[i] = constructorArgs[i].GetType();
			}

			foreach (Type type in types)
			{
				if (type.IsAbstract == true)
					continue;
				if (type.GetInterface(interfaceName) == null)
					continue;

				ConstructorInfo ci = type.GetConstructor(constructorArgTypes);
				if (ci == null)
					continue;
				RetVal.Add((TInteface)ci.Invoke(constructorArgs));
			}
			return RetVal.ToArray();
		}
	}
}